package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.mybatis.model.Message;
import com.bsh.repository.DaoMessage;
import com.bsh.repository.DaoSysConfig;
import com.bsh.service.ApiMessageService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
import com.bsh.utils.Des3;
import com.bsh.utils.Pager;
@Service
@Transactional
public class ApiMessageServiceImpl implements ApiMessageService{

	@Autowired
	DaoMessage msgDao;
	@Autowired
	DaoSysConfig sysDao;
	
    /**
     * 查询消息列表
     */
	@Override
	public ApiMessage getMsgList(JSONObject dto) throws ApiException {
		ApiMessage msg=new ApiMessage();
		Integer pageIndex = ApiUtils.objIsEmpty(dto.getInteger("pageIndex"))? 1	: dto.getInteger("pageIndex");
		Integer pageSize = ApiUtils.objIsEmpty(dto.getInteger("pageSize")) ? 10	: dto.getInteger("pageSize");
		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list=msgDao.getMessageListPage(pager, null);
		JSONObject data = new JSONObject();
		data.put("msgList", list);
		data.put("count", pager.getRowCount());
		data.put("totalPage", pager.getPageCount());
		data.put("currentPage", pageIndex);
		msg.setCode(Constants.REQ_SUCCESS);
		msg.setMessage(Constants.MSG_SUCCESS);
		msg.setData(Des3.encResponse(data));
		return msg;
	}
    /**
     * 查询消息详情
     */
	@Override
	public ApiMessage getMsgDetail(JSONObject dto) throws ApiException {
		if(ApiUtils.objIsEmpty(dto.getInteger("id"))){
			throw new ApiException(Constants.REQ_PARAM_MISS,Constants.MSG_PARAM_MISS+"id");
		}
		ApiMessage msg=new ApiMessage();
		Message result=msgDao.selectByPrimaryKey(dto.getInteger("id"));
		msg.setCode(Constants.REQ_SUCCESS);
		msg.setMessage(Constants.MSG_SUCCESS);
		msg.setData(Des3.encResponse(result));
		return msg;
	}
	/**
	 * 发送反馈意见
	 */
	@Override
	public ApiMessage appendFeedBack(JSONObject dto) throws ApiException {
		ApiMessage msg=new ApiMessage();
		if(ApiUtils.objIsEmpty(dto.getString("userName"))){
			throw new ApiException(Constants.REQ_PARAM_MISS,"缺少用户名");
		}
		if(ApiUtils.objIsEmpty(dto.getString("feedBack"))){
			throw new ApiException(Constants.REQ_PARAM_MISS,"请输入反馈意见！");
		}
		if(dto.getString("feedBack").length()>100){
			throw new ApiException(Constants.REQ_PARAM_ERROR,"反馈意见超限（不超过100字）");
		}
		try {
			sysDao.appendFeedback(dto.getString("userName"), dto.getString("feedBack"));
		} catch (Exception e) {
			throw new ApiException(Constants.SYS_ERROR,Constants.MSG_FAILED);
		}
		msg.setCode(Constants.REQ_SUCCESS);
		msg.setMessage(Constants.MSG_SUCCESS);
		return msg;
	}

	
	

}
